﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class QuanTriTaiKhoanAdmin : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    KetNoi obj = new KetNoi();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)// Ko co phan nay ko update dc
        {
            LoadGridview();
        }
    }
    protected void btnthem_Click(object sender, EventArgs e)
    {
        string tentaikhoan = txttentaikhoan.Text.ToString();
        string matkhau = txtmatkhau.Text.ToString();
        string hoten = txthoten.Text.ToString();
        string diachi = txtdiachi.Text.ToString();
        string dienthoai = txtsodt.Text.ToString();
        string email = txtemail.Text.ToString();
        obj.ThemTaiKhoanAdmin(tentaikhoan, matkhau,hoten,diachi, dienthoai, email);
        Response.Write("<script language='javascript'> alert('Bạn đã thêm thành công !' ) </script>");
        LoadGridview();
        XoaText();

    }
    protected void XoaText()
    {
        txttentaikhoan.Text = "";
        txtmatkhau.Text = "";
        txthoten.Text = "";
        txtdiachi.Text = "";
        txtsodt.Text= "";
        txtemail.Text = "";
    }
    protected void LoadGridview()
    {
        dt = obj.loadDataTable("Select * from TaiKhoan");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    protected void btnlamlai_Click(object sender, EventArgs e)
    {
        XoaText();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        LoadGridview();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        LoadGridview();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string tentaikhoan = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string matkhau = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
        string hoten = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;
        string diachi = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text;
        string dienthoai = (GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text;
        string email = (GridView1.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox).Text;
        obj.UpdateTaiKhoanAdmin(tentaikhoan, matkhau, hoten, diachi, dienthoai, email);
        Response.Write("<script language='javascript'> alert('Cập nhật thành công !' ) </script>");
        GridView1.EditIndex = -1;
        LoadGridview();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string tentaikhoan = GridView1.DataKeys[e.RowIndex].Value.ToString();
        obj.XoaTaiKhoanAdmin(tentaikhoan);
        Response.Write("<script language='javascript'> alert('Xoá thành công !' ) </script>");
        LoadGridview();
    }
}
